package gongzi;
import java.util.Random;

abstract class Employee {
    protected String name;
    protected double salary;

    // 构造方法
    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    // 抽象方法：计算工资
    public abstract double calculateSalary();
}

class SalariedEmployee extends Employee {
    // 月薪员工直接返回工资
    public SalariedEmployee(String name, double salary) {
        super(name, salary);
    }

    @Override
    public double calculateSalary() {
        return salary;  // 月薪员工的工资就是其 salary 属性
    }
}

class HourlyEmployee extends Employee {
    private double hoursWorked;  // 工作小时数
    private double hourlyRate;  // 每小时的工资

    // 构造方法
    public HourlyEmployee(String name, double hourlyRate, double hoursWorked) {
        super(name, hourlyRate);  // 将 hourlyRate 传递给父类的 salary 属性
        this.hoursWorked = hoursWorked;
    }

    @Override
    public double calculateSalary() {
        return salary * hoursWorked;  // 按工作小时数计算工资
    }
}

public class gz {
    public static void main(String[] args) {
        // 创建员工数组，假设数组大小为10
        Employee[] employees = new Employee[10];
        Random random = new Random();

        // 随机添加 SalariedEmployee 和 HourlyEmployee 对象
        for (int i = 0; i < employees.length; i++) {
            if (random.nextBoolean()) {
                // 随机创建 SalariedEmployee 对象
                double salary = random.nextInt(5000) + 3000;  // 月薪3000到8000之间
                employees[i] = new SalariedEmployee("Employee" + (i + 1), salary);
            } else {
                // 随机创建 HourlyEmployee 对象
                double hourlyRate = random.nextInt(50) + 10;  // 每小时10到60之间
                double hoursWorked = random.nextInt(40) + 10; // 每月工作10到50小时
                employees[i] = new HourlyEmployee("Employee" + (i + 1), hourlyRate, hoursWorked);
            }
        }

        // 输出每个员工的工资
        for (int i = 0; i < employees.length; i++) {
            System.out.println(employees[i].name + " - Salary: " + employees[i].calculateSalary());
        }
    }
}
